<template>
  <List :class="prefixCls" bordered :pagination="getPagination">
    <template v-for="item in getData" :key="item.Id">
      <ListItem class="list-item relative">
        <div class="absolute top-2 right-3" v-if="item.ClickClose">
          <Popconfirm
            title="消息将被永久删除，是否继续操作？"
            @confirm="confirmDelete([item.Id])"
            placement="leftBottom"
          >
            <Icon
              icon="ant-design:close-circle-outlined"
              size="20"
              class="cursor-pointer"
              color="#0960bd"
            />
          </Popconfirm>
        </div>
        <ListItemMeta>
          <template #title>
            <div class="font-bold">
              {{ item.Title }}
            </div>
          </template>

          <template #avatar>
            <Avatar
              v-if="item.FromUser"
              :src="item.FromUser.PhotoUrl ? getImgUrl(item.FromUser.PhotoUrl) : undefined"
              style="background-color: #0960bd"
              class="avatar"
            >
              {{ item.FromUser.UserName }}
            </Avatar>
          </template>

          <template #description>
            <div>{{ item.Content }}</div>
            <div
              class="mt-1"
              v-if="item.DynamicData && item.DynamicData.BillId && item.DynamicData.BillKey"
              >{{ t(`routes.menu.${item.DynamicData.BillKey}`)
              }}<a @click.stop="linkTo(item.DynamicData)" class="pl-1"
                >{{ item.DynamicData.BillId }}
              </a></div
            >
            <div class="flex justify-end text-gray-400 overflow-hidden">
              {{ dataHandle(item.SendTime, 'time') }}
            </div>
          </template>
        </ListItemMeta>
      </ListItem>
    </template>
  </List>
</template>
<script lang="ts" setup>
  import { computed, PropType, ref, watch, unref } from 'vue';
  import { INoticeMsg } from '/~/utils/wsType';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { List, Avatar, ListItem, ListItemMeta, Popconfirm } from 'ant-design-vue';
  import Icon from '/@/components/Icon';
  import { isNumber } from '/@/utils/is';
  import { getImgUrl } from '/~/utils/system/index';
  import { dataHandle } from '/~/utils/data/index';
  import { useRouter } from 'vue-router';
  import { message_delete } from '/@/api/work/system/message';
  // 多语言转换hook
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const props = defineProps({
    list: {
      type: Array as PropType<INoticeMsg[]>,
      default: () => [],
    },
    pageSize: {
      type: Number as PropType<number>,
      default: 5,
    },
    currentPage: {
      type: Number,
      default: 1,
    },
    titleRows: {
      type: Number,
      default: 1,
    },
    descRows: {
      type: Number,
      default: 2,
    },
    onTitleClick: {
      type: Function as PropType<(Recordable) => void>,
    },
  });
  const router = useRouter();
  const emits = defineEmits(['update:currentPage', 'change']);
  const { prefixCls } = useDesign('header-notify-list');
  const current = ref(props.currentPage || 1);
  const getData = computed(() => {
    const { pageSize, list } = props;
    let size = isNumber(pageSize) ? pageSize : 5;
    return list.slice(size * (unref(current) - 1), size * unref(current));
  });
  watch(
    () => props.currentPage,
    (v) => {
      current.value = v;
    },
  );
  const getPagination = computed(() => {
    const { list, pageSize } = props;
    if (pageSize > 0 && list && list.length > pageSize) {
      return {
        total: list.length,
        pageSize,
        current: unref(current),
        onChange(page) {
          current.value = page;
          emits('update:currentPage', page);
        },
      };
    } else {
      return false;
    }
  });
  function confirmDelete(ids: string[]) {
    console.log('删除');
    message_delete({ ids }).then((_) => {
      emits('change');
    });
  }
  function linkTo(record: Recordable) {
    router.push({
      name: record.BillKey + '_Detail',
      query: {
        Id: record.BillId,
      },
    });
  }
</script>
<style lang="less" scoped>
  @prefix-cls: ~'@{namespace}-header-notify-list';

  .@{prefix-cls} {
    &::-webkit-scrollbar {
      display: none;
    }

    ::v-deep(.ant-pagination-disabled) {
      display: inline-block !important;
    }

    &-item {
      padding: 6px;
      overflow: hidden;
      cursor: pointer;
      transition: all 0.3s;

      .title {
        margin-bottom: 8px;
        font-weight: normal;

        .extra {
          float: right;
          margin-top: -1.5px;
          margin-right: 0;
          font-weight: normal;

          .tag {
            margin-right: 0;
          }
        }

        .avatar {
          margin-top: 4px;
        }

        .description {
          font-size: 12px;
          line-height: 18px;
        }

        .datetime {
          margin-top: 4px;
          font-size: 12px;
          line-height: 18px;
        }
      }
    }
  }
</style>
